<?php

/**
* 指纹认证，指纹录入，指纹对比，指纹管理
*/
class FingerPrint
{

	public function __construct() {}

	private function SendCommand($content,$length,$sum,$addr='FFFFFFFFF',$sign = '01')
	{
		$SndMsg = "./SendUART " . $addr.$sign.$length.$content.$sum;
		$last_line = exec($SndMsg,$result[]);
		//print_r($result);
		return json_encode($result);
	}


	public function PS_GetImage()
	{
		return $this->SendCommand("01",'0003','0005');
	}

	public function PS_GenChar()
	{
		$this->SendCommand("01",'0003','0005');
		return $this->SendCommand("0201",'0004','0008');
	}


	public function PS_Search()
	{
		return $this->SendCommand("041h",'0003','0008');
	}

	/**
	 * 获取所有指纹数目
	 *@return: 状态和指纹总数
	 */
	public function PS_ValidTempleteNum()
	{
		$data = json_decode($this->SendCommand("1d",'0003','0021'));
		$result = array('status' => $data[0][9],'num' => $data[0][10].$data[0][11]);
		return $result;
	}

	/**
	 *  将 flash 数据库中指定 ID 号的指纹模板读入到模板缓冲 区 CharBuffer1
	 *@return 加载结果 0为正确
	 *EF01FFFFFFFF010006070100010010
	 */
	public function PS_LoadChar($id = '0009')
	{
		$this->PS_GetImage();
		$this->PS_GenChar();
		$data = json_decode($this->SendCommand("0701",'06',$id.'0024'));
		$result = array('status' => $data[0][9],'num' => $data[0][10].$data[0][11]);
		return json_encode($data);
	}

	public function PS_UpImage()
	{
		//$this->PS_GetImage();
		$enroll = $this->PS_Enroll();
		$data = json_decode($this->SendCommand("0a",'0003','000e'));
		//var_dump($data[0][0]);

		//$data['text'] = implode('',$data[0]);
		//var_dump($data['text']);
		$pattern = '|.{4}EF01FFFFFFFF020082|';
		$data['temp'] = preg_replace($pattern,'' , $data[0][0]);
		$header = "424D00FF0000000000003604000028000000000100002001000001000800000000000000000000000000000000000000000000000000000000000000000000000000030303000404040005050500060606000707070008080800090909000A0A0FF00B0B0B000C0C0C000D0D0D000E0E0E000F0F0F00101010001111110012121200131313001414140015151500161616001717170018181800191919001A1A1A001B1B1B001C1C1C001D1D1D001E1E1E001F1F1F00202020002121210022222200232323002424240025252500262626002727270028282800292929002A2A2A002B2B2B002C2C2C002D2D2D002E2E2E002F2F2F00303030003131310032323200333333003434340035353500363636003737370038383800393939003A3A3A003B3B3B003C3C3C003D3D3D003E3E3E003F3F3F00404040004141410042424200434343004444440045454500464646004747470048484800494949004A4A4A004B4B4B004C4C4C004D4D4D004E4E4E004F4F4F00505050005151510052525200535353005454540055555500565656005757570058585800595959005A5A5A005B5B5B005C5C5C005D5D5D005E5E5E005F5F5F00606060006161610062626200636363006464640065656500666666006767670068686800696969006A6A6A006B6B6B006C6C6C006D6D6D006E6E6E006F6F6F00707070007171710072727200737373007474740075757500767676007777770078787800797979007A7A7A007B7B7B007C7C7C007D7D7D007E7E7E007F7F7F00808080008181810082828200838383008484840085858500868686008787870088888800898989008A8A8A008B8B8B008C8C8C008D8D8D008E8E8E008F8F8F00909090009191910092929200939393009494940095959500969696009797970098989800999999009A9A9A009B9B9B009C9C9C009D9D9D009E9E9E009F9F9F00A0A0A000A1A1A100A2A2A200A3A3A300A4A4A400A5A5A500A6A6A600A7A7A700A8A8A800A9A9A900AAAAAA00ABABAB00ACACAC00ADADAD00AEAEAE00AFAFAF00B0B0B000B1B1B100B2B2B200B3B3B300B4B4B400B5B5B500B6B6B600B7B7B700B8B8B800B9B9B900BABABA00BBBBBB00BCBCBC00BDBDBD00BEBEBE00BFBFBF00C0C0C000C1C1C100C2C2C200C3C3C300C4C4C400C5C5C500C6C6C600C7C7C700C8C8C800C9C9C900CACACA00CBCBCB00CCCCCC00CDCDCD00CECECE00CFCFCF00D0D0D000D1D1D100D2D2D200D3D3D300D4D4D400D5D5D500D6D6D600D7D7D700D8D8D800D9D9D900DADADA00DBDBDB00DCDCDC00DDDDDD00DEDEDE00DFDFDF00E0E0E000E1E1E100E2E2E200E3E3E300E4E4E400E5E5E500E6E6E600E7E7E700E8E8E800E9E9E900EAEAEA00EBEBEB00ECECEC00EDEDED00EEEEEE00EFEFEF00F0F0F000F1F1F100F2F2F200F3F3F300F4F4F400F5F5F5F6F6F600F7F7F700F8F8F800F9F9FAFF";

		$tail = "0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0E000F0F0F0F0000000000000F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0E0F0F0F0F0E0F0F0F0F0F0F0E0F0E0F0E0E0E0E0E0F0E0E0F0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0F0F0F0E0F0E0F0F0F0E0F0E0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0";

		$enroll['img'] = $header.$data['temp'].$tail;
		return $enroll;

	}

	/**
	*		自动注册
	*@return:array(status => 状态码，成功为0, id => 指纹id) 所录入指纹的存储空间ID  0表示建立指纹成功
	*/
	public function PS_Enroll()
	{
		$data = json_decode($this->SendCommand("10",'0003','0014'));
		$result = array('status' => $data[0][9],'id' => $data[0][11]);
		return $result;
	}
	/**
	 *自动比对指纹
	 *@return array(status，result) 验证成功返回status == 0, result为指纹id
	 */
	public function PS_Identify()
	{
		$data = json_decode($this->SendCommand("11",'0003','0015'));
		$result = array('status' => $data[0][9],'id' => $data[0][11]);
		return $result;
	}
	/**
	 * 高速搜索
	 */
	public function PS_HighSpeedSearch()
	{
		return $this->SendCommand("1B",'0003','0019');
	}

	/**
	*清空指纹库
	*@return 是否清空成功 成功返回0
	*/
	 public function PS_Empty()
	{
		$data = json_decode($this->SendCommand("0D",'0003','0011'));
		$result = (int)$data[0][9];
		return $result;
	}
}
